package com.gmall.rabbitmq.consumer.service;

import com.gmall.rabbitmq.config.RabbitMQConfig_WorkQueue;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class RabbitMQServive_WorkQueue {

    @RabbitHandler
    @RabbitListener(queues = RabbitMQConfig_WorkQueue.Queue_Work)
    public void receive_SimpleMessage1(Channel channal, Message message,String msg){

        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            System.out.println("------ RabbitMQConfig_WorkQueue Start-------");
            System.out.println("MessageID:" + message.getMessageProperties().getMessageId());
            System.out.println("MessageBody:" + new String(message.getBody()));
            System.out.println("msg:" + msg);
            System.out.println("------ RabbitMQConfig_WorkQueue End-------");
            channal.basicAck(deliveryTag,false);
        }catch (Exception e){
            //拒绝消息
            try {
                channal.basicNack(deliveryTag,false,true);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}
